package org.epam.szhuk.overlay;

import java.util.List;

import org.epam.szhuk.greenway.model.StoredGeoPoint;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.util.Log;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.Projection;

/**
 * To draw path as lines
 * 
 * mapView.invalidate() have to be called after it
 * 
 * @author Sergii Zhuk
 *
 */
public class PathOverlay extends Overlay {
	private static final String TAG="PathOverlay";
	public static final int LINE_WIDTH=6;
	Projection projection;
	List<StoredGeoPoint> storedGeoDataList;
	GeoPoint[] geoPointArr;
	Point[] pointArr;
	int maxZoomLevel;
	int currentZoomLevel;
	
		
	public PathOverlay(List<StoredGeoPoint> storedGeoDataList, Projection projection, int maxZoomLevel,int currentZoomLevel) {
		this.projection=projection;
		this.storedGeoDataList= storedGeoDataList;
		geoPointArr=new GeoPoint[storedGeoDataList.size()];
		pointArr=new Point[storedGeoDataList.size()];
		this.maxZoomLevel=maxZoomLevel;
		this.currentZoomLevel=currentZoomLevel;
	}
	
	public void setZoomLevel(int currentZoomLevel){
		this.currentZoomLevel=currentZoomLevel;
	}
	@Override
	public void draw(Canvas canvas, MapView mapv, boolean shadow) {
		super.draw(canvas, mapv, shadow);
		
		Paint mPaint=generatePaint();
		
		if(geoPointArr.length>1){
			geoPointArr[0]=new GeoPoint(storedGeoDataList.get(0).getLat(),storedGeoDataList.get(0).getLon());
			pointArr[0]=new Point();
			projection.toPixels(geoPointArr[0], pointArr[0]);
			for(int i=1; i<storedGeoDataList.size(); i++){
				geoPointArr[i]=new GeoPoint(storedGeoDataList.get(i).getLat(),storedGeoDataList.get(i).getLon());
				pointArr[i]=new Point();
				projection.toPixels(geoPointArr[i], pointArr[i]);
				
				if((storedGeoDataList.get(i).getPointType()==StoredGeoPoint.POINT_TYPE_EMPTY)
						||(storedGeoDataList.get(i).getPointType()==StoredGeoPoint.POINT_TYPE_APPROXIMATED)){
					mPaint.setStyle(Style.STROKE);
					int coef=maxZoomLevel-currentZoomLevel+1;
					mPaint.setPathEffect(new DashPathEffect(new float[] {2*coef, 2*coef}, (float) (1.5*coef))); //TODO add dynamycally changes coefs for different distances between points
					//Log.w(TAG, "Stroke path effect applied on coordinate i="+i);
				}
				else{
					mPaint.setStyle(Style.FILL_AND_STROKE);
					mPaint.setPathEffect(null);
					//mPaint.setPathEffect(new DashPathEffect(new float[] {0,0}, 0));
				}
				canvas.drawLine(pointArr[i-1].x+LINE_WIDTH/2, pointArr[i-1].y+LINE_WIDTH/2, pointArr[i].x+LINE_WIDTH/2, pointArr[i].y+LINE_WIDTH/2, mPaint);
			}
		}
		else{
			Log.w(TAG, "GeoPointArr length<2, can't draw path lines");
		}
	}
	
	private Paint generatePaint(){
		Paint mPaint=new Paint();
		mPaint.setDither(true);
		
		mPaint.setColor(Color.RED);
		mPaint.setAlpha(170);
		//mPaint.setARGB(120, r, g, b);
		mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
		mPaint.setStrokeJoin(Paint.Join.ROUND);
		mPaint.setStrokeCap(Paint.Cap.ROUND);
		mPaint.setStrokeWidth(LINE_WIDTH);
		mPaint.setPathEffect(new DashPathEffect(new float[] {0,0}, 0));
		return mPaint;
	}
}
